package com.huitone.gddw.controller.analysis;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.common.excel.CreateExcel;
import com.huitone.gddw.controller.business.SendSmController;
import com.huitone.gddw.controller.common.CommonController;
import com.huitone.gddw.log.Log;
import com.huitone.gddw.log.LogResult;
import com.huitone.gddw.log.LogType;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.analysis.ISmSubmitFailService;
import com.huitone.gddw.service.dto.analysis.SmSubmitFailDetailDTO;
import com.huitone.gddw.service.dto.analysis.SmSubmitFailDetailDTO.AllDetail;
import com.huitone.gddw.service.dto.analysis.SmSubmitFailDetailDTO.GateAllDetail;
//import com.sun.xml.internal.messaging.saaj.packaging.mime.util.QPDecoderStream;
import com.huitone.gddw.service.dto.analysis.SmSubmitFailDTO;
import com.huitone.gddw.service.dto.analysis.SmSubmitFailDTO.AllCount;
import com.huitone.gddw.service.dto.analysis.SmSubmitFailDTO.GateAllCount;

@Controller
@RequestMapping(value = "smSubmitFail")
public class SmSubmitFailController extends CommonController {

	private final static Logger LOGGER = Logger.getLogger(SendSmController.class);

	@Autowired
	private ISmSubmitFailService smSubmitFailService;

	@ResponseBody
	@RequestMapping(value = "querySvrCount")
	@Log(logType = LogType.SELECT, content = "查询短信发送失败统计")
	public HashMap querySvrCount(QueryPage qp, HttpServletRequest request) {
		Map<String, Object> params = getParams(request);
		params.put("type", "svr");
		try {
			qp.setParams(params);
			return smSubmitFailService.queryPageCount(qp);
		} catch (Exception e) {
			LOGGER.error("查询短信发送失败统计出现异常：" + e.getMessage(), e);
			request.setAttribute("logResult", LogResult.EXCEPTION);
		}
		return null;

	}

	private Map<String, Object> getParams(HttpServletRequest request) {
		Map<String, Object> params = new HashMap<>();
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		String svrNodes = request.getParameter("svrNodes");
		String gateLinkIds = request.getParameter("gateLinkIds");
		String errorTypes = request.getParameter("errorType");

		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("svrNodes", svrNodes);
		params.put("gateLinkIds", gateLinkIds);
		params.put("errorTypes", errorTypes);
		return params;
	}

	@ResponseBody
	@RequestMapping(value = "queryGateCount")
	@Log(logType = LogType.SELECT, content = "查询短信发送失败统计")
	public HashMap queryGateCount(QueryPage qp, HttpServletRequest request) {
		HashMap result = null;
		Map<String, Object> params = getParams(request);
		// 表示类型
		params.put("type", "gate");
		qp.setParams(params);

		try {
			result = smSubmitFailService.queryPageCount(qp);
		} catch (Exception e) {
			LOGGER.error("查询短信发送失败统计出现异常：" + e.getMessage(), e);
			// request.setAttribute("logResult", LogResult.EXCEPTION);
		}
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "querySvrDetail")
	public HashMap querySvrDetail(QueryPage qp, HttpServletRequest request) {

		Map<String, Object> params = getParams(request);

		params.put("type", "svr");
		qp.setParams(params);
		try {
			return smSubmitFailService.queryPageCountDetail(qp);
		} catch (Exception e) {
			LOGGER.error("查询短信发送失败明细出现异常：" + e.getMessage(), e);
			return null;
		}
	}

	@ResponseBody
	@RequestMapping(value = "queryGateDetail")
	public HashMap queryGateDetail(QueryPage qp, HttpServletRequest request) {

		Map<String, Object> params = getParams(request);
		params.put("type", "gate");
		qp.setParams(params);
		try {
			return smSubmitFailService.queryPageCountDetail(qp);
		} catch (Exception e) {
			LOGGER.error("查询短信发送失败明细出现异常：" + e.getMessage(), e);
			return null;
		}
	}

	/**
	 * 导出 Excel
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/out-excel-svrCount", method = RequestMethod.POST)
	public void outExcelSvrCount(HttpServletResponse response, HttpServletRequest request) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String fileName = "业务系统短信发送失败统计表";

		Map<String, Object> params = getParams(request);

		List<AllCount> list = null;
		params.put("type", "svr");
		list = (List) smSubmitFailService.queryCount(params).get("allCount");

		if (null == list) {
			list = new ArrayList<AllCount>();
			list.add(new SmSubmitFailDTO.AllCount());
		}
		if (0 == list.size()) {
			list.add(new SmSubmitFailDTO.AllCount());
		}

		HSSFWorkbook wb = (HSSFWorkbook) CreateExcel.getInstance().CreateNewExcelNoTemplate(fileName, list)
				.getWorkbook();
		OutputStream fOut = null;
		try {
			fileName = new String(fileName.getBytes("gbk"), "ISO8859-1");
			response.reset();
			response.setContentType("application/octet-stream; charset=utf-8");
			response.setHeader("Content-Disposition",
					"attachment;filename=" + fileName + sdf.format(new Date()) + ".xls");
			fOut = response.getOutputStream();
			wb.write(fOut);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fOut.flush();
				fOut.close();
			} catch (IOException e) {

			}
		}
	}

	@RequestMapping(value = "/out-excel-gateCount", method = RequestMethod.POST)
	public void outExcelGateCount(HttpServletResponse response, HttpServletRequest request) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String fileName = "通道短信发送失败统计表";
		Map<String, Object> params = getParams(request);
		List<GateAllCount> list = null;
		params.put("type", "gate");
		list = (List) smSubmitFailService.queryCount(params).get("gateAllCount");

		if (null == list) {
			list = new ArrayList<>();
			list.add(new SmSubmitFailDTO.GateAllCount());
		}
		if (0 == list.size()) {
			list.add(new SmSubmitFailDTO.GateAllCount());
		}

		HSSFWorkbook wb = (HSSFWorkbook) CreateExcel.getInstance().CreateNewExcelNoTemplate(fileName, list)
				.getWorkbook();
		OutputStream fOut = null;
		try {
			fileName = new String(fileName.getBytes("gbk"), "ISO8859-1");
			response.reset();
			response.setContentType("application/octet-stream; charset=utf-8");
			response.setHeader("Content-Disposition",
					"attachment;filename=" + fileName + sdf.format(new Date()) + ".xls");
			fOut = response.getOutputStream();
			wb.write(fOut);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fOut.flush();
				fOut.close();
			} catch (IOException e) {

			}
		}
	}

	/**
	 * 导出 Excel
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/out-excel-svrDetail", method = RequestMethod.POST)
	public void outExcelSvrDetail(HttpServletRequest request, HttpServletResponse response) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String fileName = "业务系统短信发送失败明细表";

		Map<String, Object> params = getParams(request);

		List<AllDetail> list = null;
		params.put("type", "svr");
		list = (List) smSubmitFailService.queryCountDetail(params).get("allDetail");
		if (null == list) {
			list = new ArrayList<>();
			list.add(new SmSubmitFailDetailDTO.AllDetail());
		}
		if (0 == list.size()) {
			list.add(new SmSubmitFailDetailDTO.AllDetail());
		}

		HSSFWorkbook wb = (HSSFWorkbook) CreateExcel.getInstance().CreateNewExcelNoTemplate(fileName, list)
				.getWorkbook();
		OutputStream fOut = null;
		try {
			fileName = new String(fileName.getBytes("gbk"), "ISO8859-1");
			response.reset();
			response.setContentType("application/octet-stream; charset=utf-8");
			response.setHeader("Content-Disposition",
					"attachment;filename=" + fileName + sdf.format(new Date()) + ".xls");
			fOut = response.getOutputStream();
			wb.write(fOut);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fOut.flush();
				fOut.close();
			} catch (IOException e) {

			}
		}
	}

	@RequestMapping(value = "/out-excel-gateDetail", method = RequestMethod.POST)
	public void outExcelGateDetail(HttpServletRequest request, HttpServletResponse response) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String fileName = "通道短信发送失败明细表";

		Map<String, Object> params = getParams(request);

		List<GateAllDetail> list = null;

		params.put("type", "gate");
		list = (List) smSubmitFailService.queryCountDetail(params).get("gateAllDetail");
		if (null == list) {
			list = new ArrayList<>();
			list.add(new SmSubmitFailDetailDTO.GateAllDetail());
		}

		if (0 == list.size()) {
			list.add(new SmSubmitFailDetailDTO.GateAllDetail());
		}

		HSSFWorkbook wb = (HSSFWorkbook) CreateExcel.getInstance().CreateNewExcelNoTemplate(fileName, list)
				.getWorkbook();
		OutputStream fOut = null;
		try {
			fileName = new String(fileName.getBytes("gbk"), "ISO8859-1");
			response.reset();
			response.setContentType("application/octet-stream; charset=utf-8");
			response.setHeader("Content-Disposition",
					"attachment;filename=" + fileName + sdf.format(new Date()) + ".xls");
			fOut = response.getOutputStream();
			wb.write(fOut);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fOut.flush();
				fOut.close();
			} catch (IOException e) {

			}
		}
	}

}
